var jqWindow = function(){
	this.jqwType = ["popup", "save"];
	this.jqwDiv = null;
	this.jqwOpts = new Object();
};

jqWindow.prototype.intializer = function(){
	this.destroyJqwAll();
};

jqWindow.prototype.jqwSet = function(opts){
	this.jqwOpts = opts;	
};

jqWindow.prototype.run = function(){
	this.destroyDivPopup();
	this.jqwDiv = $("<div />").attr("id", this.jqwOpts["jqwId"]).css("display", "none");
	this.loadJqWindow();
};

jqWindow.prototype.fileLoadJqWindow = function(){
	var obj = this;
	var ajax = new jqMyAjax();
	var option = new Object();
	option["url"] = obj.jqwOpts["url"];
	option["dataType"] = obj.jqwOpts["dataType"];
	option["type"] = obj.jqwOpts["type"];
	option["data"] = obj.jqwOpts["data"];
	option["processData"] = false,
	option["contentType"] = false,
    option["success"] = function(data){
		if(data != null && data != "undefined"){
			if(data.model.result){
				alert("Success!");
			}else{
				alert("Failure!");
			}
		}else{
			alert("Failure!");
		}
		
		obj.destroyJqwSelected();
    };
	option["complete"] = function(){ 
		ajax.uiUnBlock();
	};
	option["error"] = function(xhr, status, thrown){
		var extraMsg = "";
		var sessionOutRedirect = true;
		ajax.errorCalback(xhr, extraMsg, sessionOutRedirect);
	};
	
	ajax.callAjaxFileUp(option);
};

jqWindow.prototype.loadJqWindow = function(){
	var obj = this;
	var ajax = new jqMyAjax();
	var data = obj.jqwOpts["data"];
	var option = new Object();
	option["url"] = obj.jqwOpts["url"];
	option["dataType"] = obj.jqwOpts["dataType"];
	option["type"] = obj.jqwOpts["type"];
	option["data"] = (data != null && data.indexOf("frm:") != -1)? $("#"+data.split("frm:")[1]).serialize() : data;
	option["success"] = function(data){
		if(obj.jqwOpts["jqwType"] == obj.jqwType[0]){//pop
			$(obj.jqwDiv).html(data);
			obj.getWindow();
			 
		}else if(obj.jqwOpts["jqwType"] == obj.jqwType[1]){//save
			alert("Success!");
			
			obj.triggerObj();
			obj.destroyJqwSelected();

		}else{
			//alert("You didn't choose option.");
		}
	};
	option["complete"] = function(){ 
		ajax.uiUnBlock();
	};
	option["error"] = function(xhr, status, thrown){
		var extraMsg = "";
		var sessionOutRedirect = true;
		ajax.errorCalback(xhr, extraMsg, sessionOutRedirect);
	};
	
	ajax.callAjax(option);
};

jqWindow.prototype.getWindow = function(){
	var titleFontColor = this.validOpts(this.jqwOpts["titleFontColor"], "gray");
	var title = "<font style='color:"+titleFontColor+"'>" + this.validOpts(this.jqwOpts["title"], "New") + "</font>";
	var resizable = this.jqwOpts["resizable"];
	var maximizable = this.jqwOpts["maximizable"];
	var minimizable = this.jqwOpts["minimizable"];
	if(!resizable){
		maximizable = resizable;
		minimizable = resizable;
	}		
	
	var option = new Object();
	option["title"] = title;
	option["content"] = $(this.jqwDiv);
	option["showFooter"] = this.validOpts(this.jqwOpts["showFooter"], false);
	option["footerContent"] = this.validOpts(this.jqwOpts["footerContent"], "");
	option["showRoundCorner"] = this.validOpts(this.jqwOpts["showRoundCorner"], true);
	option["draggable"] = this.validOpts(this.jqwOpts["draggable"], true);
	option["resizable"] = this.validOpts(resizable, true);
	option["showModal"] = this.validOpts(this.jqwOpts["showModal"], true);
	option["maximizable"] = this.validOpts(maximizable, true);
	option["minimizable"] = this.validOpts(minimizable, true);
	option["scrollable"] = this.validOpts(this.jqwOpts["scrollable"], true);
	option["width"] = parseInt(this.validOpts(this.jqwOpts["width"], "200"));
	option["height"] = parseInt(this.validOpts(this.jqwOpts["height"], "200"));
	option["maxWidth"] = parseInt(this.validOpts(this.jqwOpts["width"], "200"));
	option["maxHeight"] = parseInt(this.validOpts(this.jqwOpts["height"], "200"));
	option["onShow"] = function(wnd){
		var w = ($(window).width()/2) - (parseInt(option["width"])/2);
		var h = ($(window).height()/2) - (parseInt(option["height"])/2);
		w = (w <= 0)? 50:w;
		h = (h <= 0)? 50:h;
		wnd.move(w, h);
	};

	$.window(option);
};

jqWindow.prototype.validOpts = function(value, initValue){
	var rtn = initValue;

	if(value != null && value != "undefined" && value != initValue){
		rtn = value;
	}else{
		rtn = initValue;
	}
	
	return rtn;
};

jqWindow.prototype.destroyJqwSelected = function(){
	var closeOpts = this.jqwOpts["jqwClose"];
	if(closeOpts == null || closeOpts == "undefined") closeOpts = true;
	
	if(closeOpts){
		var w = $.window.getSelectedWindow();
		if($(w).length > 0) w.close();
	}
};

jqWindow.prototype.destroyJqwAll = function(){
	$.window.closeAll();
};

jqWindow.prototype.destroyDivPopup = function(){
	if(this.jqwDiv != null){
		log($(this.jqwDiv).length);
		$(this.jqwDiv).remove();
	}
};

jqWindow.prototype.triggerObj = function(){
	var trgObj = this.jqwOpts["triggerObj"];
	if(trgObj != null && trgObj != "undefined" && trgObj != ""){
		if($(trgObj).length > 0) $(trgObj).trigger("click");
	}
};

